package com.igo.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.igo.domain.datacollection.devicedata.FlatCarData;
import com.igo.domain.datacollection.result.Result;
import com.igo.mapper.devicedata.DeviceInfoMapper;
import com.igo.mapper.devicedata.FlatCarDataMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/flatCar")
public class FlatCarController {

    @Resource
    private DeviceInfoMapper deviceInfoMapper;

    @Resource
    private FlatCarDataMapper flatCarDataMapper;


    /**
     * 查询所有平板车最新位置(选中的车)
     * @return
     */
    @GetMapping("/location/list")
    @ResponseBody
    public Result locationList(@RequestParam String deviceNo) {
        List<Map<String, FlatCarData>> data =new ArrayList<>();
        if (StringUtils.isNotBlank(deviceNo)){
            List<String> deviceNoList = Arrays.stream(deviceNo.split(",")).collect(Collectors.toList());
            Date date = DateUtil.date();
            Date weekAgo = DateUtil.offset(date, DateField.MINUTE, -7);
            List<FlatCarData> flatCarDataList = flatCarDataMapper.selectList(new LambdaQueryWrapper<FlatCarData>()
                    .ge(FlatCarData::getDataTime,weekAgo).le(FlatCarData::getDataTime,date).in(FlatCarData::getDeviceNo,deviceNoList).orderByDesc(FlatCarData::getDataTime));
            List<String> flatCarList = flatCarDataList.stream().map(FlatCarData::getDeviceNo).distinct().collect(Collectors.toList());
            flatCarList.stream().forEach(flatCarData->{
                FlatCarData car = flatCarDataList.stream().filter(flatCar -> flatCar.getDeviceNo().equals(flatCarData)).findFirst().orElse(null);
                if (car!=null){
                    Map<String, FlatCarData> map = new HashMap<String, FlatCarData>();
                    map.put(flatCarData,car);
                    data.add(map);
                }
            });
        }else {
            Date date = DateUtil.date();
            Date weekAgo = DateUtil.offset(date, DateField.MINUTE, -7);
            List<FlatCarData> flatCarDataList = flatCarDataMapper.selectList(new LambdaQueryWrapper<FlatCarData>()
                    .ge(FlatCarData::getDataTime,weekAgo).le(FlatCarData::getDataTime,date).orderByDesc(FlatCarData::getDataTime));
            List<String> flatCarList = flatCarDataList.stream().map(FlatCarData::getDeviceNo).distinct().collect(Collectors.toList());
            flatCarList.stream().forEach(flatCarData->{
                FlatCarData car = flatCarDataList.stream().filter(flatCar -> flatCar.getDeviceNo().equals(flatCarData)).findFirst().orElse(null);
                if (car!=null){
                    Map<String, FlatCarData> map = new HashMap<String, FlatCarData>();
                    map.put(flatCarData,car);
                    data.add(map);
                }
            });
        }
        return Result.success(data);
    }
}
